// file
const devConfig = require('./config/webpack.dev.config.js');
const proConfig = require('./config/webpack.pro.config.js');

// utils
const path = require('path');
const { merge } = require('webpack-merge');
const glob = require('glob');

// plugin
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader');

// for RTF
const CopyWebpackPlugin = require('copy-webpack-plugin');

// extract CSS
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

const entry = {};
const paths = glob.sync('src/pages/*/main.js');
const pageNames = paths.map(i => path.basename(path.dirname(i)));
pageNames.forEach((val, idx) => {
  entry[val] = path.join(__dirname, paths[idx]);
});

const HtmlPlugins = [];

pageNames.forEach(page => {
  HtmlPlugins.push(new HtmlWebpackPlugin({
    title: 'ToKnow',
    template: path.join(__dirname, 'templates', `${page}.html`),
    filename: `${page}.html`,
    chunks: [page]
  }))
});

const baseConfig = {
  entry,
  output: {
    path: path.join(__dirname, 'dist'),
    publicPath: '/',
    filename: 'js/[name].bundle.js',
    chunkFilename: 'js/chunk.[chunkhash].js',
    clean: true,
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@c': path.resolve(__dirname, 'src', 'components'),
      '~': path.resolve(__dirname, 'src-mobile')
    }
  },
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|gif|webp)$/i,
        type: "asset/resource",
        generator: {
          filename: "images/[name].[hash:5][ext]", // 独立的配置
        },
      },
      // svg 文件
      {
        test: /\.svg$/i,
        type: "asset/inline",
      },
      // 字体文件
      {
        test: /\.(otf|eot|woff2?|ttf)$/i,
        type: "asset/inline"
      },
      // 数据文件
      {
        test: /\.(txt|xml)$/i,
        type: "asset/source", // exports the source code of the asset
      },
      {
        test: /\.vue$/,
        use: 'vue-loader'
      },
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          // 'style-loader',
          'css-loader',
          'postcss-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          MiniCssExtractPlugin.loader,
          // 'style-loader',
          'css-loader',
          'postcss-loader',
          'less-loader',
        ]
      },
      {
        test: /\.sass$/,
        use: [
          MiniCssExtractPlugin.loader,
          // 'style-loader',
          'css-loader',
          'postcss-loader',
          'sass-loader',
        ]
      }
    ]
  },
  plugins: [
    new VueLoaderPlugin(),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, 'public'),
          to: path.resolve(__dirname, 'dist'),
        }
      ],
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name].[contenthash:5].css',
      chunkFilename: 'css/[id].[contenthash].css',
      ignoreOrder: true
    }),
  ].concat(HtmlPlugins),
  optimization: {
    splitChunks: {
      chunks: 'all',
      hidePathInfo: true,
    },
  }
};

module.exports = (env, argv) => {
  if(argv.mode === 'development') {
    return merge(baseConfig, devConfig)
  } else if(argv.mode === 'production') {
    return merge(baseConfig, proConfig);
  } else {
    return baseConfig;
  }
}